<meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<?php
	include("conn.php");
	if ($_POST["username"] == "")
	{
		echo "<script>alert('用户名不能为空！');history.go(-1);</script>";
	}
	else if ($_POST["password1"] == "")
	{
		echo "<script>alert('密码不能为空！');history.go(-1);</script>";
	}
	else if ($_POST["password2"] == "")
	{
		echo "<script>alert('请确认密码！');history.go(-1);</script>";
	}
	else if ($_POST["email"] == "")
	{
		echo "<script>alert('邮箱不能为空！');history.go(-1);</script>";
	}
	else if (!(preg_match('/^[\w\x80-\xff]{3,15}$/', $_POST["username"]))) {
		echo "<script>alert('错误：用户名不符合规定！');history.go(-1);</script>";
	}
	else if (strlen($_POST["password1"]) < 6)
	{
		echo "<script>alert('错误：密码长度不符合规定');history.go(-1);</script>";
	}
	else if (!(preg_match("/^[a-z0-9]+([_\.-]*[a-z0-9]+)*@[a-z0-9]([-]?[a-z0-9]+)\.[a-z]{2,3}(\.[a-z]{2})?$/i",$_POST["email"])))
	{
		echo "<script>alert('错误：电子邮箱格式错误！');history.go(-1);</script>";
	}
	else
	{
		$password1 = $_POST["password1"];
		$password2 = $_POST["password2"];
		$sql = "SELECT username FROM user WHERE username = '$_POST[username]'";
		$result1 = mysql_query($sql);
		$num1 = mysql_num_rows($result1);
		$sql = "SELECT email FROM user WHERE email = '$_POST[email]'";
		$result2 = mysql_query($sql);
		$num2 = mysql_num_rows($result2);
		if ($password1 != $password2)
		{
			echo "<script>alert('两次密码输入不一致！');history.go(-1);</script>";
		}
		if ($num1)
		{
			echo "<script>alert('用户名已存在，请重新输入！');history.go(-1);</script>";
		}
		if ($num2)
		{
			echo "<script>alert('邮箱已被注册，请重新输入！');history.go(-1);</script>";
		}
		$count = 0;
		$sql = "INSERT INTO user(id,username,password,email,time,count)
					VALUES (NULL, '$_POST[username]','$_POST[password1]','$_POST[email]',now(), $count)";		
		mysql_query($sql);echo mysql_error();
		echo "注册成功，正在跳转...";
		echo "<meta http-equiv='refresh' content='3	; url = index.php'>";
	}
?>